package com.wjy.controller;

import com.wjy.common.jwt.JwtToken;
import com.wjy.common.result.ResultBuilder;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ybxxszl
 * @desc JwtToken控制
 * @date 2019/5/4
 */
@Log4j2
@RestController
public class TokenController {

    @Autowired
    private JwtToken jwtToken;

    @RequestMapping(value = "/getToken", method = RequestMethod.GET)
    public ResultBuilder getToken(@RequestParam(value = "id") String id) {
        try {
            String token = jwtToken.getToken(id);
            log.info("getToken: token - " + token);
            return ResultBuilder.success(token);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e);
            return ResultBuilder.error(e);
        }
    }

    @RequestMapping(value = "/verifyToken", method = RequestMethod.GET)
    public ResultBuilder verifyToken(@RequestParam(value = "token") String token) {
        try {
            String id = jwtToken.verifyToken(token);
            log.info("verifyToken: id - " + id);
            return ResultBuilder.success(id);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e);
            return ResultBuilder.error(e);
        }
    }

    @RequestMapping(value = "/refreshToken", method = RequestMethod.GET)
    public ResultBuilder refreshToken(@RequestParam(value = "token") String token) {
        try {
            String t = jwtToken.refreshToken(token);
            log.info("refreshToken: token - " + t);
            return ResultBuilder.success(t);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e);
            return ResultBuilder.error(e);
        }
    }

}
